package com.csu.income;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;

import com.csu.bean.Income;
import com.csu.dao.IncomeDao;
import com.csu.dao.IncomeDaoImpl;
import com.csu.income.*;

public class IncomePanel extends JPanel{
	/**
	 *
	 */
//	private static final long serialVersionUID = 1L;
	private JTable table;
	private String[] columnNames;//表头
	private Object[][] rowData;//表格的数据
	private DefaultTableModel model;//表格显示的数据模型
	private JPanel panelLow;
	private JPanel northPanel = new JPanel();
	private JPanel westPanel;
	private int curPage=1;//当前页数
	private int pageSize=20;//显示条数
	private int totalCount;//总共页数
	private JButton prevBtn;
	private JButton nextBtn;
	private JLabel northLabel;
	private JLabel curPagelabel;
	private JLabel totalPagelabel;
	private JLabel curPagelabel1;
	private JLabel totalPagelabel1;
	private JButton prevBtn1;
	private JButton nextBtn1;
	private JButton addBtn;
	private JButton deleteBtn;
	private JButton selectBtn;
	private JButton changeBtn;
	private JButton returnBtn;
	private JComboBox<String> typeBox;
	private IncomeDao incomeDao = new IncomeDaoImpl();

	private JTable table1;
	private String[] columnNames1;//表头
	private Object[][] rowData1;//表格的数据
	private DefaultTableModel model1;//表格显示的数据模型
	private int totalCount1;//总共页数
	private JPanel panelLow1;
	private Box cBox;
	private Box westBox;
	private Box westBox1;
	private Box westBox2;


	public IncomePanel(){
		//设置边界布局
		setLayout(new BorderLayout(10,10));
//        northPanel.setBackground(new Color(241,198,197));

	}

	/**
	 * 初始化表格数据
	 */
	public void initData(){
		columnNames = new String[]{"收入编号","收入者","收入类型","收入日期","收入来源","收入金额","相关备注"};
		rowData = getRowData(incomeDao.getIncomeByPage(0,pageSize));
		//总页数
		totalCount = (int)Math.ceil(((IncomeDao) incomeDao).getIncomeCount()/(1.0*pageSize));
		model = new DefaultTableModel(rowData, columnNames);
		table = new JTable();
		table.setModel(model);
		//设置表格的内容相关
		table.setFont(new Font(null,Font.BOLD,14));
		table.setRowHeight(30);
//		RowSorter <TableModel> rowSorter = new TableRowSorter<TableModel>(model);
//		table.setRowSorter(rowSorter);
		cBox = Box.createVerticalBox();
		cBox.add(table.getTableHeader());
		cBox.add(table);
		//表格内容添加在中部
		add(cBox,BorderLayout.CENTER);
	}

	public void initDataByType(String s) {
		columnNames1 = new String[]{"收入编号","收入者","收入类型","收入日期","收入来源","收入金额","相关备注"};
		rowData1 = getRowData(incomeDao.getIncomeByType(s));
		//总页数
		totalCount1 = (int)Math.ceil(((IncomeDao) incomeDao).getIncomeCountByType(s)/(1.0*pageSize));
		model1 = new DefaultTableModel(rowData1, columnNames1);
		table1 = new JTable();
		table1.setModel(model1);
		//设置表格的内容相关
		table1.setFont(new Font(null,Font.BOLD,14));
		table1.setRowHeight(30);
//		RowSorter <TableModel> rowSorter = new TableRowSorter<TableModel>(model1);
//		table1.setRowSorter(rowSorter);
		cBox = Box.createVerticalBox();
		cBox.add(table1.getTableHeader());
		cBox.add(table1);
		//表格内容添加在中部
		add(cBox,BorderLayout.CENTER);
	}

	private Object[][] getRowData(List<Income> list){
		if(list!=null && list.size()>0){
			Object[][] data = new Object[list.size()][7];
			for(int i=0;i<list.size();i++){
				data[i][0] = list.get(i).getItem();
				data[i][1] = list.get(i).getEarner();
				data[i][2] = list.get(i).getIncomeType();
				data[i][3] = list.get(i).getIncomeDate();
				data[i][4] = list.get(i).getSource();
				data[i][5] = list.get(i).getAmount();
				data[i][6] = list.get(i).getRemarks();
			}
			return data;
		}
		return null;
	}

	/**
	 * 新建按钮
	 */
	public void initButton() {
//		setBackground(new Color(241,198,197));
//		180,180,182
//		36,42,147
//		200,222,240
//		241,198,197
		addBtn = new JButton("新建收入");
		addBtn.setSize(40, 20);
		deleteBtn = new JButton("删除收入");
		deleteBtn.setSize(40, 20);
		selectBtn = new JButton("查询收入");
		selectBtn.setSize(40, 20);
		changeBtn = new JButton("修改收入");
		changeBtn.setSize(40, 20);
		returnBtn = new JButton("返回全部");
		returnBtn.setSize(40, 20);
		returnBtn.setVisible(false);
		typeBox = new JComboBox<String>();
		String salary = "工资";
		String stock = "股票";
		String dividend = "分红";
		String bonus = "奖金";
		typeBox.addItem(salary);
		typeBox.addItem(stock);
		typeBox.addItem(dividend);
		typeBox.addItem(bonus);
		typeBox.setSelectedItem(salary);
		typeBox.setPreferredSize(new Dimension(83,25));

		addBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				INewFrame iNewFrame = new INewFrame();
				iNewFrame.setSize(500, 450);
				iNewFrame.setLocationRelativeTo(null);
				iNewFrame.initContent();

			}
		});

		deleteBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					Object itemO = table.getValueAt(table.getSelectedRow(), 0);
					int item = Integer.parseInt("" + itemO);
					IncomeDao incomeDao = new IncomeDaoImpl();
					incomeDao.delIncomeById(item);
					model.removeRow(table.getSelectedRow());
				} catch (Exception exception) {
					JOptionPane.showMessageDialog(null,"删除失败","删除收入记录",JOptionPane.ERROR_MESSAGE);
				}
			}
		});


		changeBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				IncomeDao updateIncome = new IncomeDaoImpl();
				model.getRowCount();
				int res = 0;
				Object[][] c = new Object[model.getRowCount()][model.getColumnCount()];
				for(int i = 0;i < model.getRowCount();i++){
					c[i][0] = model.getValueAt(i,0);
					c[i][1] = model.getValueAt(i,1);
					c[i][2] = model.getValueAt(i,2);
					c[i][3] = model.getValueAt(i,3);
					c[i][4] = model.getValueAt(i,4);
					c[i][5] = model.getValueAt(i,5);
					c[i][6] = model.getValueAt(i,6);
				}
				for(int i = 0;i < model.getRowCount();i++) {
					res = updateIncome.updateIncome(Integer.parseInt("" + c[i][0]), String.valueOf(c[i][1]), String.valueOf(c[i][2]), Date.valueOf(String.valueOf(c[i][3])), String.valueOf(c[i][4]), new BigDecimal(String.valueOf(c[i][5])), String.valueOf(c[i][6])) + res;
				}
				if(res == model.getRowCount()){
					IChangeFrame iChangeFrame = new IChangeFrame();
					iChangeFrame.setSize(300, 150);
					iChangeFrame.setLocationRelativeTo(null);
					iChangeFrame.initContent();
				}else{
					JOptionPane.showMessageDialog(null,"修改失败","修改收入记录",JOptionPane.ERROR_MESSAGE);				}
			}
		});

		selectBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String s = typeBox.getSelectedItem().toString();
				table.getTableHeader().setVisible(false);
				table.setVisible(false);
				initDataByType(s);
				returnBtn.setVisible(true);
				selectBtn.setEnabled(false);
				panelLow.setVisible(false);
				initPage1();
				panelLow1.setVisible(true);



			}
		});

		returnBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				initData();
				initPage();
				table.getTableHeader().setVisible(true);
				table.setVisible(true);
				table1.getTableHeader().setVisible(false);
				table1.setVisible(false);
				selectBtn.setEnabled(true);
				returnBtn.setVisible(false);
				panelLow.setVisible(true);
				panelLow1.setVisible(false);



			}
		});
//        northPanel = new JPanel();
//        northPanel.setBackground(new Color(241,198,197));
		westBox = Box.createVerticalBox();
		westBox.setBackground(new Color(200 ,222 ,248));
		westBox1 = Box.createVerticalBox();
		westBox2 = Box.createVerticalBox();

		westBox1.add(addBtn);
		westBox1.add(Box.createVerticalStrut(20));
		westBox1.add(deleteBtn);
		westBox1.add(Box.createVerticalStrut(20));
		westBox1.add(changeBtn);
		westBox1.add(Box.createVerticalStrut(20));
		westBox.add(westBox1);

		westPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
//		westPanel.setBackground(new Color(241,198,197));
		westPanel.add(typeBox);
		westPanel.add(Box.createHorizontalStrut(2));
		westBox.add(westPanel);
//		westBox.add(Box.createVerticalStrut(400));
//		eastBox2.add(Box.createVerticalStrut(20));
		westBox2.add(selectBtn);
		westBox2.add(Box.createVerticalStrut(20));
		westBox2.add(returnBtn);
		westBox.add(westBox2);

		add(westBox,BorderLayout.WEST);


	}

	/**
	 * 分页组件
	 */

	public void initPage(){
		//定义工具栏
//		northPanel = new JPanel();
		prevBtn = new JButton("上一页");
		nextBtn = new JButton("下一页");
		northLabel = new JLabel();
		northLabel.setText("");
		northLabel.setFont(new Font(null,Font.BOLD,17));
		curPagelabel =new JLabel();
		curPagelabel.setText("第  1");
		totalPagelabel = new JLabel();
		totalPagelabel.setText(" /   " + totalCount +"  页");

		prevBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(curPage>1) {
					curPage--;
					curPagelabel.setText("第  " + curPage+"");
					//从数据库获取当前页的记录
					List<Income> list = incomeDao.getIncomeByPage((curPage-1)*pageSize, pageSize);
					rowData = getRowData(list);
					model = new DefaultTableModel(rowData,columnNames);
					table.setModel(model);
				}
				setPageStatus();

			}
		});

		nextBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(curPage<totalCount) {
					curPage++;
					curPagelabel.setText("第  " + curPage+"");
					//从数据库获取当前页的记录
					List<Income> list = incomeDao.getIncomeByPage((curPage-1)*pageSize, pageSize);
					rowData = getRowData(list);
					model = new DefaultTableModel(rowData,columnNames);
					table.setModel(model);
				}
				setPageStatus();
			}
		});

		northPanel.add(northLabel);
//        northPanel.setBackground(new Color(241,198,197));
		add(northPanel,BorderLayout.NORTH);
		panelLow = new JPanel();
//        toolBar.setPreferredSize(new Dimension(400,200));
		panelLow.add(prevBtn);
		panelLow.add(Box.createHorizontalStrut(20));
		panelLow.add(curPagelabel);
		panelLow.add(totalPagelabel);
		panelLow.add(Box.createHorizontalStrut(20));
		panelLow.add(nextBtn);
//        panelLow.setBackground(new Color(180,180,182));
		panelLow.setPreferredSize(new Dimension(1050,130));
		setPageStatus();
		add(panelLow,BorderLayout.SOUTH);

	}

	private void setPageStatus() {
		if(curPage==1){
			//如果当前已经是第一页
			prevBtn.setEnabled(false);
		}else{
			prevBtn.setEnabled(true);
		}

		if(curPage==totalCount){
			nextBtn.setEnabled(false);
		}else {
			nextBtn.setEnabled(true);
		}
	}

	public void initPage1(){
		//定义工具栏
		prevBtn1 = new JButton("上一页");
		nextBtn1 = new JButton("下一页");
		curPagelabel1 =new JLabel();
		curPagelabel1.setText("第  1");
		totalPagelabel1 = new JLabel();
		totalPagelabel1.setText(" /   " + totalCount1 +"  页");

		prevBtn1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(curPage>1) {
					curPage--;
					curPagelabel1.setText("第  " + curPage+"");
					//从数据库获取当前页的记录
					List<Income> list = incomeDao.getIncomeByPage((curPage-1)*pageSize, pageSize);
					rowData1 = getRowData(list);
					model1 = new DefaultTableModel(rowData1,columnNames1);
					table1.setModel(model1);
				}
				setPageStatus1();

			}
		});
		nextBtn1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(curPage<totalCount1) {
					curPage++;
					curPagelabel1.setText("第  " + curPage+"");
					//从数据库获取当前页的记录
					List<Income> list = incomeDao.getIncomeByPage((curPage-1)*pageSize, pageSize);
					rowData1 = getRowData(list);
					model1 = new DefaultTableModel(rowData1,columnNames1);
					table1.setModel(model1);
				}
				setPageStatus1();
			}
		});

		panelLow1 = new JPanel();
//        panelLow1.setBackground(new Color(241,198,197));
		panelLow1.add(prevBtn1);
		panelLow1.add(Box.createHorizontalStrut(20));
		panelLow1.add(curPagelabel1);
		panelLow1.add(totalPagelabel1);
		panelLow1.add(Box.createHorizontalStrut(20));
		panelLow1.add(nextBtn1);
//        panelLow1.setBackground(new Color(180,180,182));
		panelLow1.setPreferredSize(new Dimension(1050,130));
		setPageStatus1();
		add(panelLow1,BorderLayout.SOUTH);

	}

	private void setPageStatus1() {
		if(curPage==1){
			//如果当前已经是第一页
			prevBtn1.setEnabled(false);
		}else{
			prevBtn1.setEnabled(true);
		}

		if(curPage==totalCount1){
			nextBtn1.setEnabled(false);
		}else {
			nextBtn1.setEnabled(true);
		}
	}

}



